package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueBO;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pengyehua
 * @date 2020/12/6 20:16
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService{

    @Autowired
    IssueMapper issueMapper;

    /**
     * 模糊查询问题
     * @param issueBO
     * @return
     */
    @Override
    public ListData<Issue> queryIssue(IssueBO issueBO) {
        //分页
        PageHelper.startPage(issueBO.getPage(),issueBO.getLimit());

        //模糊查询
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if(issueBO.getQuestion() != null){
            criteria.andQuestionLike("%" + issueBO.getQuestion() + "%");
        }
        //排序
        issueExample.setOrderByClause(issueBO.getSort() + " " + issueBO.getOrder());

        List<Issue> issues = issueMapper.selectByExample(issueExample);
        ListData<Issue> issueListData = new ListData<>();
        issueListData.setItems(issues);

        //获取查询的条目数
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        int total =(int) issuePageInfo.getTotal();
        issueListData.setTotal(total);
        return issueListData;
    }

    /**
     * 添加问题
     * @param issue
     * @return
     */
    @Override
    public Issue insertIssue(Issue issue) {
        Date date = new Date();
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        issue.setDeleted(false);

        issueMapper.insertSelective(issue);
        return issue;
    }

    /**
     * 修改问题
     * @param issue
     * @return
     */
    @Override
    public Issue updateIssue(Issue issue) {
        Date date = new Date();
        issue.setUpdateTime(date);
        issueMapper.updateByPrimaryKeySelective(issue);
        return issue;
    }

    /**
     * 删除问题
     * @param issue
     */
    @Override
    public void deleteIssue(Issue issue) {
        issue.setDeleted(true);
        issueMapper.updateByPrimaryKeySelective(issue);
    }
}
